<?php

namespace Admin\Controller;

class NodeController extends AdminController {



	#列表

	public function Index() {

		$node = M('Node')->where('level=1')->order('sort asc , id asc')->select();

		foreach ($node as $key => $value) {

			$Controller = M('Node')->where('pid='.$value['id'])->select();

			$node[$key]['child'] = $Controller;

		}

		$this->assign('node',$node);

		$this->display();



	}



	#获得子节点

	public function getChild(){

		$pid = I('get.pid');

		$map['pid'] = $pid;

		$list = M('Node')->field('id,name,title')->where($map)->select();

		$this->assign('list',$list);

		$result = $this->fetch();

		echo $result;

	}



	#添加模型

	function Add() {

		$level = I('get.level');

		$pid = I('get.pid');

		if ($level==1) {

			$nodeName = 'Application';

		}elseif($level==2){

			$nodeName = 'Model';

		}elseif($level==3){

			$nodeName = 'Action';

		}



		$map['id'] = $pid;

		$pname = M('Node')->where($map)->getField('title');



		$this->assign('nodeName',$nodeName);		

		$this->assign('level',$level);

		$this->assign('pname',$pname);

		$this->assign('pid',$pid);

		$this->display();

	}



	#保存

	function save() {

		$obj = D('Node');

		if ($data = $obj->create()) {

			if ($obj->add($data)) {

				$map['pid'] = $data['pid'];

				$list = M('Node')->where($map)->order('sort asc , id asc')->select();

				$this->assign('list',$list);

				$url = $this->fetch('option');

				$state = 'SUCCESS';

	            $message = 'Successful operation';

			} else {

				$state = 'Operation failed';

			}

		} else {

			$state = $obj->getError();

		}

		echo $this->echo_json_str($state,$message,$url);

	}



	#编辑

	function edit() {

		$id = (int) $_GET['id'];

		if (!isset ($id)) {

			$this->error('Parameter error');

		}



		$obj = M('Node');

		$list = $obj->where("id=$id")->find();

		if (!$list) {

			$this->error('Information does not exist');

		} else {

			if ($list['pid']==1) {

				$nodeName = 'Application';

			}elseif($list['pid']==2){

				$nodeName = 'Model';

			}elseif($list['pid']==3){

				$nodeName = 'Action';

			}

			$map['id']=$list['pid'];

			$pname = M('Node')->where($map)->getField('title');

			$this->assign('pname',$pname);

			$this->assign('list', $list);

			$this->display();

		}



	}



	#更新

	function update() {

		$obj = D('Node');

		if ($data = $obj->create()) {

			if ($obj->save($data)) {

				$state = 'SUCCESS';

	            $message = 'Successful operation';

	            

	            $map['pid'] = $data['pid'];

				$list = M('Node')->where($map)->order('sort asc , id asc')->select();

				$this->assign('list',$list);

				$url = $this->fetch('option');

			} else {

				$state = 'Operation failed';

			}

		} else {

			$state = $obj->getError();

		}

		echo $this->echo_json_str($state,$message,$url);



	}



	#删除

	function del() {



		$id = I('get.nodeID');

		if(!isset($id)){

			echo 'Please select the node to delete';die;

		}



		$obj = M('Node');

		$list = $obj->where('pid='.$id)->find();

		if($list){

			echo 'Please delete the child node first';die;

		}



		$list = $obj->where('id='.$id)->delete();



		if($list){

			echo '1';

		}else{

			echo 'Delete failed';

		}

	}

	

}

?>